<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:rll:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['project:rll:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="rllList"
      border
      stripe
      :row-style="{ padding: '0px' }"
      :cell-style="{ padding: '0px' }"
      :header-row-style="{ padding: '2px' }"
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日期" align="center" prop="rllRq" width="150">
        <template #default="scope">
          <span>{{ parseTime(scope.row.rllRq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产星期" align="center" prop="rllWeekday" />
      <el-table-column label="生产周期" align="center" prop="rllWeek" />
      <el-table-column label="是否休息" align="center" prop="rllFlag">
        <template #default="scope">
          <el-switch
            v-model="scope.row.rllFlag"
            active-value="1"
            inactive-value="0"
            @change="handleRllFlag(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        show-overflow-tooltip
        align="center"
        prop="remark"
      />
      <el-table-column label="更新人员" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改生产日历对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="400px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="是否休息">
          <el-radio-group v-model="form.rllFlag">
            <el-radio
              v-for="dict in rllFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="720px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="12">
          <el-form-item label="日期" prop="rllRq">
            <el-date-picker
              clearable
              size="small"
              v-model="queryParams.rllRq"
              type="month"
              :clearable="false"
              value-format="yyyy-MM"
              placeholder="选择日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产星期" prop="rllWeekday">
            <el-input
              v-model="queryParams.rllWeekday"
              placeholder="请输入生产星期"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-form-item label="是否休息" prop="rllFlag">
          <el-select
            v-model="queryParams.rllFlag"
            placeholder="请选择是否休息"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in rllFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-col :span="12">
          <el-form-item label="生产周期" prop="rllWeek">
            <el-input
              v-model="queryParams.rllWeek"
              placeholder="请输入生产周期"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12"></el-col>
        <el-col :span="24">
          <el-form-item
            style="display: flex; margin-top: 20px; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRll,
  getRll,
  delRll,
  addRll,
  updateRll,
  exportRll,
  changeRllFlag,
} from "@/api/project/rll";

export default {
  name: "Rll",
  data() {
    return {
      openSearch: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产日历表格数据
      rllList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否休息字典
      rllFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 31,
        rllRq: this.moment().format("YYYY-MM"),
        rllWeekday: null,
        rllWeek: null,
        rllFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rllRq: [{ required: true, message: "日期不能为空", trigger: "blur" }],
        rllWeekday: [
          { required: true, message: "生产星期不能为空", trigger: "blur" },
        ],
        rllWeek: [
          { required: true, message: "生产周期不能为空", trigger: "blur" },
        ],
        rllFlag: [
          { required: true, message: "是否休息不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_y_n").then((response) => {
      this.rllFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询生产日历列表 */
    getList() {
      this.loading = true;
      listRll(this.queryParams).then((response) => {
        this.rllList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openSearch = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rllId: null,
        rllRq: null,
        rllWeekday: null,
        rllWeek: null,
        rllFlag: "0",
        remark: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.rllId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产日历";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rllId = row.rllId || this.ids;
      getRll(rllId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产日历";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.rllId != null) {
            updateRll(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRll(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rllIds = row.rllId || this.ids;
      this.$confirm(
        '是否确认删除生产日历编号为"' + rllIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delRll(rllIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有生产日历数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportRll(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    handleSearch() {
      this.reset();
      this.title = "查询生产日历";
      this.openSearch = true;
    },
    /*修改是否休息*/
    handleRllFlag(row) {
      let text = row.rllFlag === "0" ? "工作状态" : "休息状态";

      this.$confirm('确认修改为"' + text + "吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return changeRllFlag(row.rllId, row.rllFlag);
        })
        .then(() => {
          this.getList();
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.rllFlag = row.rllFlag === "0" ? "1" : "0";
        });
    },
  },
};
</script>
